% Solve an Input-Output Fitting problem with a Neural Network
% Script generated by Neural Fitting app
% Created 21-Feb-2017 17:46:52
%
% This script assumes these variables are defined:
%
%   explain - input data.
%   this - target data.

clear all

for year = 1995 : 2012
    
    for conflict = 2 : 3
        
        for within = 0 : 1
        

            ach = 'train_neural%d_%d_onset.csv';        
            filename = sprintf(ach, year, conflict);
            M = csvread(filename);
            ach = 'test_neural%d_%d_onset.csv';        
            filename = sprintf(ach, year, conflict);
            M2 = csvread(filename);

            %randomize order of trainings sample        
            soso = size(M);
            
            if within == 0
                finalone = soso(2)-1;                
            else
                finalone = 15;
            end
            
            %create new M
            Mp = M(:, 1 : finalone);
            Mq = M(:, soso(2));
            
            M = horzcat(Mp, Mq);
            
            
             %create new M
            Mp = M2(:, 1 : finalone);
            Mq = M2(:, soso(2));
            
            M2 = horzcat(Mp, Mq);

            %randomize order of trainings sample        
            r1 = rand(length(M),1);
            Mx = horzcat(M, r1);
            ei = size(Mx);
            Mx = sortrows(Mx, ei(2));

            M = Mx(:, 1 : ei(2) - 1);



            explain = M(:, 1 : finalone);
            this = M(:, finalone + 1);

            testit = M2(:, 1 : finalone);
            %testit = testit';
            this2 = M2(:, finalone + 1);

            x = explain';
            t = this';



            that = [this; this2];

            thetas = [explain; testit];
            thetas = thetas';

            t = that';

            % Choose a Training Function
            % For a list of all training functions type: help nntrain
            % 'trainlm' is usually fastest.
            % 'trainbr' takes longer but may be better for challenging problems.
            % 'trainscg' uses less memory. Suitable in low memory situations.
            trainFcn = 'trainlm';  % Levenberg-Marquardt backpropagation.

            % Create a Fitting Network
            hiddenLayerSize = 10;
            net = fitnet(hiddenLayerSize,trainFcn);

            % Setup Division of Data for Training, Validation, Testing
            net.divideFcn = 'dividerand';  % Divide data randomly
            net.divideMode = 'sample';  % Divide up every sample

            [trainInd,valInd,testInd] = ...
            divideind(length(this),round(0.7 * length(this)),length(this) - round(0.7 * length(this)), length(this) + 1 : length(that));


            %net.divideParam.trainRatio = 70/100;
            %net.divideParam.valRatio = 15/100;
            %net.divideParam.testRatio = 15/100;



            % Train the Network
            [net,tr] = train(net,thetas,t);

            % Test the Network
            %I think I will have to feed in out of sample data instead of x
            % y = net(x);
            % e = gsubtract(t,y);
            % performance = perform(net,t,y);

            y = net(thetas);

            future = y(length(this) + 1 : length(that)); 
            future = future';


            if within == 0
                ach = 'output/future_neural%d_%d_onset_overall.csv';
            else
                ach = 'output/future_neural%d_%d_onset_within.csv';
            end


            
            %prison_delta
            filename = sprintf(ach, year, conflict);
            display(filename)

            csvwrite(filename, future)

    
        end

    end

end





e = gsubtract(that,y);
performance = perform(net,that,y);


%

% View the Network
view(net)

% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotfit(net,x,t)

